package com.zk.controller;


import com.zk.entity.ResultDto;
import com.zk.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Api
@RestController
public class SmsController {
    @Autowired
    @Qualifier("smsServiceImpl")
    private SmsService smsService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @ApiOperation(value = "向目标手机发送短信数据", notes = "输入电话后就可以进行操作处理")
    @ApiImplicitParam(required = true,name = "phone",paramType = "path",dataType = "String")
    @GetMapping("/Ssm/{phone}")
    public ResultDto<String> sendSsm(@PathVariable String phone){
        if(redisTemplate.opsForHash().get(phone,"code")!=null){
            return new ResultDto<>(500, "验证码未过期，可以使用");

        }


//        if(redisTemplate.opsForValue().get("code")!=null)
//        {
//            return new ResultDto<>(500, "验证码未过期，可以使用");
//
//
//        }
        String code = UUID.randomUUID().toString().substring(0,4);
        Map<String,String> params =new HashMap<>();
        params.put("code",code);
        params.put("phone",phone);

        if(smsService.smsSend(params)){
            Map<String,String> map=new HashMap<>();
            map.put(phone,code);
            redisTemplate.opsForHash().put(phone,"code",code);
            redisTemplate.expire(phone,60,TimeUnit.SECONDS);
//            redisTemplate.opsForValue().set("code",code,5,TimeUnit.SECONDS);
            return new ResultDto<>(200, "验证码以发送");
        }
        return new ResultDto<>(500, "系统繁忙请稍后重试");


    }
}
